package com.jerei.plugin.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jerei.plugin.api.IRoleService;
import com.jerei.plugin.dao.IWcmCommonRoleDao;
import com.jerei.plugin.entity.WcmCommonRole;
import com.jerei.plugin.service.IWcmCommonRoleService;
import com.jerei.utils.condition.ConditionEntity;
import com.jerei.utils.condition.ConditionOp;
import com.jerei.utils.condition.WhereEntity;
import com.jerei.utils.spring.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ro.fortsoft.pf4j.Extension;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by sunfeihu on 2016/10/21.
 */
@Extension
@Service
public class WcmCommonRoleServiceImpl implements IWcmCommonRoleService, IRoleService {

    @Autowired
    private IWcmCommonRoleDao iWcmCommonRoleDao;

    @Override
    public WcmCommonRole save(WcmCommonRole entity) {
        return iWcmCommonRoleDao.save(entity);
    }

    @Override
    public void saveRoleForAdminuser(Long adminuserId, Long[] roleIds) {
        //先清除管理员的所有角色
        iWcmCommonRoleDao.clearRoleForAdminuser(adminuserId);
        //在重新添加
        iWcmCommonRoleDao.saveRoleForAdminuser(adminuserId, roleIds);
    }

    @Override
    public int delete(Long id) {
        return iWcmCommonRoleDao.delete(id);
    }

    @Override
    public int update(WcmCommonRole entity) {
        return iWcmCommonRoleDao.update(entity);
    }

    @Override
    public WcmCommonRole find(Long id) {
        return iWcmCommonRoleDao.find(id);
    }


    @Override
    public void save(List<WcmCommonRole> list) {
        iWcmCommonRoleDao.save(list);
    }

    @Override
    public void delete(List<Long> ids) {
        iWcmCommonRoleDao.delete(ids);
    }

    @Override
    public void update(List<WcmCommonRole> list) {
        iWcmCommonRoleDao.update(list);
    }

    @Override
    public PageBean<WcmCommonRole> page(int nowPage, int pageSize, ConditionEntity conditionEntity) {
        return iWcmCommonRoleDao.page(nowPage, pageSize, conditionEntity);
    }

    @Override
    public PageBean<WcmCommonRole> pageByDev(int nowPage, int pageSize) {
        ConditionEntity conditionEntity = new ConditionEntity();
        List<WhereEntity> whereEntities = new ArrayList<WhereEntity>();
        WhereEntity whereEntity = new WhereEntity();
        whereEntity.setField("gid");
        whereEntity.setValue("null");
        whereEntity.setOp(ConditionOp.eq);
        whereEntities.add(whereEntity);
        whereEntity = new WhereEntity();
        whereEntity.setField("isShow");
        whereEntity.setValue("1");
        whereEntity.setOp(ConditionOp.eq);
        whereEntities.add(whereEntity);
        whereEntity = new WhereEntity();
        whereEntity.setField("menuId");
        whereEntity.setValue("1");
        whereEntity.setOp(ConditionOp.eq);
        whereEntities.add(whereEntity);
        conditionEntity.setWhereEntities(whereEntities);
        return iWcmCommonRoleDao.page(nowPage, pageSize, conditionEntity);
    }

    @Override
    public List<WcmCommonRole> findRolesByAdminuser(Long adminuserId) {
        return iWcmCommonRoleDao.findRolesByAdminuser(adminuserId);
    }

    @Override
    public JSONArray showDevRoles() {
        JSONArray array = null;
        List<WcmCommonRole> rootRoles = iWcmCommonRoleDao.rootDevRolesShow();
        if (null != rootRoles && rootRoles.size() > 0) {
            JSONArray children = null;
            JSONObject obj = null;
            array = new JSONArray();
            for (WcmCommonRole r : rootRoles) {
                obj = (JSONObject) JSONObject.toJSON(r);
                obj.put("text", r.getName());
                children = this.childrenShow(r.getId());
                if (null != children) {
                    obj.put("children", children);
                }
                array.add(obj);
            }
        }
        return array;
    }

    private JSONArray childrenShow(Long pid) {
        List<WcmCommonRole> roles = iWcmCommonRoleDao.rolesShowByParent(pid);
        JSONArray array = null;
        if (null != roles && roles.size() > 0) {
            JSONArray children = null;
            JSONObject obj = null;
            array = new JSONArray();
            for (WcmCommonRole r : roles) {
                obj = (JSONObject) JSONObject.toJSON(r);
                obj.put("text", r.getName());
                children = this.childrenShow(r.getId());
                if (null != children) {
                    obj.put("children", children);
                }
                array.add(obj);
            }
        }
        return array;
    }

    @Override
    public JSONArray allDevRoles() {
        JSONArray array = null;
        List<WcmCommonRole> rootRoles = iWcmCommonRoleDao.rootDevRolesAll();
        if (null != rootRoles && rootRoles.size() > 0) {
            JSONArray children = null;
            JSONObject obj = null;
            array = new JSONArray();
            for (WcmCommonRole r : rootRoles) {
                obj = (JSONObject) JSONObject.toJSON(r);
                obj.put("text", r.getName());
                children = this.childrenAll(r.getId());
                if (null != children) {
                    obj.put("children", children);
                }
                array.add(obj);
            }
        }
        return array;
    }

    private JSONArray childrenAll(Long pid) {
        List<WcmCommonRole> roles = iWcmCommonRoleDao.rolesAllByParent(pid);
        JSONArray array = null;
        if (null != roles && roles.size() > 0) {
            JSONArray children = null;
            JSONObject obj = null;
            array = new JSONArray();
            for (WcmCommonRole r : roles) {
                obj = (JSONObject) JSONObject.toJSON(r);
                obj.put("text", r.getName());
                children = this.childrenAll(r.getId());
                if (null != children) {
                    obj.put("children", children);
                }
                array.add(obj);
            }
        }
        return array;
    }

    @Override
    public JSONArray allGroupRoles(Long gid) {
        JSONArray array = null;
        List<WcmCommonRole> rootRoles = iWcmCommonRoleDao.rootGroupRolesAll(gid);
        if (null != rootRoles && rootRoles.size() > 0) {
            JSONArray children = null;
            JSONObject obj = null;
            array = new JSONArray();
            for (WcmCommonRole r : rootRoles) {
                obj = (JSONObject) JSONObject.toJSON(r);
                obj.put("text", r.getName());
                children = this.childrenAll(r.getId());
                if (null != children) {
                    obj.put("children", children);
                }
                array.add(obj);
            }
        }
        return array;
    }

    @Override
    public JSONArray showGroupRoles(Long gid) {
        JSONArray array = null;
        List<WcmCommonRole> rootRoles = iWcmCommonRoleDao.rootGroupRolesShow(gid);
        if (null != rootRoles && rootRoles.size() > 0) {
            JSONArray children = null;
            JSONObject obj = null;
            array = new JSONArray();
            for (WcmCommonRole r : rootRoles) {
                obj = (JSONObject) JSONObject.toJSON(r);
                obj.put("text", r.getName());
                children = this.childrenShow(r.getId());
                if (null != children) {
                    obj.put("children", children);
                }
                array.add(obj);
            }
        }
        return array;
    }
}
